/*	This program produces Table D.1 */

***** Set directories 
local dir_clean 	"~/Dropbox/Retirement gaming/clean"
local dir_output 	"~/Dropbox/Retirement gaming/output/dataverse"


use "`dir_clean'/mainsample_age50analysis.dta", clear

* Sets of controls		
local listcontrols1 = " i.t  " 
local listcontrols2 = " i.t i.ciiu2_1stobs i.ndep_cat_1stobs i.ndep_cat " 
local listcontrols3 = " i.t i.ciiu2_1stobs i.ndep_cat_1stobs i.ndep_cat i.year#i.i.ciiu2_1stobs i.year#i.ndep_cat_1stobs "  

** TABLE D.1, Panel A **
estimates clear
*Self-employed
foreach c in 1 2 3  {
	preserve
	keep if  self_empl==1 
	*Normalize wages
	sum W if age_centered==-1
	local meanw=r(mean)
	g Wz=W/`meanw'
	* Regress
	reghdfe Wz pre48 post5053 post54, absorb(`listcontrols3') 
	g thissample=e(sample)
	eststo: reghdfe Wz pre48 post5053 post54 if thissample==1, absorb(`listcontrols`c'') vce(cluster i )
	restore
}
* Small firms
foreach c in 1 2 3 {
*Keep those observed at least X% of the time
	preserve
	keep if empl==1 & small_1stobs==1 
	*Normalize wages
	sum W if age_centered==-1 
	local meanw=r(mean)
	g Wz=W/`meanw'
	* Regress
	reghdfe Wz pre48 post5053 post54, absorb(`listcontrols3') 
	g thissample=e(sample)
	eststo: reghdfe Wz pre48 post5053 post54  if thissample==1, absorb(`listcontrols`c'') vce(cluster i )
	restore
}	
* Large firms
foreach c in 1 2 3 {
*Keep those observed at least X% of the time
	preserve
	keep if empl==1 & small_1stobs==0
	*Normalize wages
	sum W if age_centered==-1 
	local meanw=r(mean)
	g Wz=W/`meanw'
	* Regress
	reghdfe Wz pre48 post5053 post54, absorb(`listcontrols3') 
	g thissample=e(sample)
	eststo: reghdfe Wz pre48 post5053 post54  if thissample==1, absorb(`listcontrols`c'') vce(cluster i )
	restore
}	 
esttab using "`dir_output'/tableD1a.tex", ///
	keep(pre* post* )  noobs sfmt(%9.0fc) ///
	b(%4.3f) se(%4.3f) star(* 0.10 ** 0.05 *** 0.01) ///
	f nomtitles  nogaps replace label nonumbers nolines	

** TABLE D.1, Panel B **
estimates clear	
*Self-employed
foreach c in  1 2 3 {
	preserve
	keep if  self_empl==1 
	*Normalize wages
	sum W if age_centered==-1
	local meanw=r(mean)
	g Wz=W/`meanw'
	* Regress
	reghdfe Wz aget post50 aget_post50, absorb(`listcontrols3') 
	g thissample=e(sample)
	eststo: reghdfe Wz aget post50 aget_post50 if thissample==1, absorb(`listcontrols`c'') vce(cluster i )
	restore
}
* Small firms
foreach c in 1 2 3 {
	preserve
	keep if  empl==1 & small_1stobs==1 
	*Normalize wages
	sum W if age_centered==-1 
	local meanw=r(mean)
	g Wz=W/`meanw'
	* Regress
	reghdfe Wz aget post50 aget_post50  , absorb(`listcontrols3') 
	g thissample=e(sample)
	eststo: reghdfe Wz aget post50 aget_post50  if thissample==1, absorb(`listcontrols`c'') vce(cluster i )
	restore
}
* Large firms
foreach c in 1 2 3 {
	preserve
	keep if  empl==1 & small_1stobs==0
	*Normalize wages
	sum W if age_centered==-1 
	local meanw=r(mean)
	g Wz=W/`meanw'
	* Regress
	reghdfe Wz aget post50 aget_post50  , absorb(`listcontrols3') 
	g thissample=e(sample)
	eststo: reghdfe Wz aget post50 aget_post50  if thissample==1, absorb(`listcontrols`c'') vce(cluster i )
	restore
}
esttab using "`dir_output'/tableD1b.tex", ///
	keep( post* age*)  stats(N N_clust1, label("Observations" "Individuals") fmt(%9.0gc))  ///
	b(%4.3f) se(%4.3f) star(* 0.10 ** 0.05 *** 0.01) ///
	f nomtitles  nogaps replace label nonumbers nolines prefoot(\hline) 	


clear all
exit
